package com.bmw.accesscount.service.impl;

import com.bmw.accesscount.dao.RNewsDao;
import com.bmw.accesscount.model.RNews;
import com.bmw.accesscount.service.RankService;
import com.bmw.accesscount.util.bean.RankResp;
import com.bmw.accesscount.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Classname: RankServiceImpl
 * Package: com.bmw.accesscount.service.impl
 * Description:
 *
 * @Author 余集楠
 * @Create 2024/6/13 0013 15:59
 * @Version 1.8
 */
@Service
@Slf4j
@Transactional
public class RankServiceImpl implements RankService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    RNewsDao rNewsDao;
    @Override
    public List<RankResp> list1() {
        String key="rank:list";
        Set<ZSetOperations.TypedTuple<Object>> set=redisUtil.zrevrangeByScoreWithScores(key,0,10);
        Iterator<ZSetOperations.TypedTuple<Object>> it=set.iterator();
        int rank=1;
        List<RankResp> list=new ArrayList<>();
        while(it.hasNext()){
            ZSetOperations.TypedTuple<Object> e=it.next();
            RankResp resp=new RankResp();
            RNews news=rNewsDao.selectByPrimaryKey(Integer.valueOf(e.getValue().toString()));
            resp.setNewsName(news.getNewsName());
            resp.getRank(rank);
            list.add(resp);
            rank++;
        }
        return list;
    }
}
